import type { EditorType } from "@base/Editor/types";
import type { enterItemsType, expressionItemsType } from "../handleBase/types";
import { keyKindEnum } from "../component/enum";
import { BracketL, BracketR, Add, Divide, Minus, Multiply, Surplus } from "./initBaseElement";
import { Clear, Equal, Pop } from "./initEditorElement";
import { OperatorType } from "@base/Operator/types";
import { BracketType } from "@base/Bracket/types";

const indexMap: Map<keyKindEnum, enterItemsType | EditorType> = new Map();

indexMap.set(keyKindEnum.add, Add);
indexMap.set(keyKindEnum.divide, Divide);
indexMap.set(keyKindEnum.minus, Minus);
indexMap.set(keyKindEnum.multiply, Multiply);
indexMap.set(keyKindEnum.surplus, Surplus);
indexMap.set(keyKindEnum.left, BracketL);
indexMap.set(keyKindEnum.right, BracketR);

indexMap.set(keyKindEnum.equal, Equal);
indexMap.set(keyKindEnum.pop, Pop);
indexMap.set(keyKindEnum.clear, Clear);

indexMap.set(keyKindEnum.zoro, '0');
indexMap.set(keyKindEnum.one, '1');
indexMap.set(keyKindEnum.two, '2');
indexMap.set(keyKindEnum.three, '3');
indexMap.set(keyKindEnum.four, '4');
indexMap.set(keyKindEnum.five, '5');
indexMap.set(keyKindEnum.six, '6');
indexMap.set(keyKindEnum.seven, '7');
indexMap.set(keyKindEnum.eight, '8');
indexMap.set(keyKindEnum.nine, '9');
indexMap.set(keyKindEnum.numDot, '.')

/**
 * 
 * @param keyDomInfo 从dom中获取的，标记该点击元素所属类型的，字符串
 * @returns 被点击元素对应的的元素，数字、小数点都是字符串形式
 */
export function translateEnterKind(keyDomInfo: string): enterItemsType | EditorType | undefined {
    return indexMap.get(keyDomInfo as keyKindEnum);
}

/**
 * 
 * @param expression 中缀表达式数组
 * @param separator 把数组拼接为字符串的分隔符
 * @returns 把中缀表达式数组翻译为对应的字面量式子
 */
export function translateExpressionToText(expression: (enterItemsType | EditorType | number)[], separator: string = '') {
    return expression.map(item => {
        if (['string','number'].includes(typeof (item))) {
            return item;
        }
        return (item as OperatorType | EditorType | BracketType).getQuote();
    })
        .join(separator);

}
